package com.guocheng.modules.room.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.dictionarie.utils.Dictionaries;
import com.guocheng.modules.room.model.po.Room;
import com.guocheng.modules.room.serive.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    /**
     * 跳转会议室管理页面
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "room/list";
    }


    /**
     * 分页查询会议室信息列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult viewsRoom(@RequestParam("page")Integer page, @RequestParam("limit")Integer limit){
        IPage<Room> roomIPage = roomService.selectPage(page,limit);
        return  AjaxResult.ok().data(roomIPage.getRecords()).total(roomIPage.getTotal());
    }

    /**
     * 打开增加页面
     * @return
     */
    @GetMapping("/show")
    public String show(@RequestParam(value = "id",required = false)Integer id,Model model){
        if(id!=null){
            Room room = roomService.selectById(id);
            model.addAttribute("room",room);
        }
        model.addAttribute("roomTypes", Dictionaries.getDictionaries("roomType"));
        return "room/show";
    }

    /**
     * 增加提交
     * @param room
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult createRoom(Room room){
        boolean insert = roomService.insert(room);
        if(insert){
            return AjaxResult.ok("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 更改提交
     * @param room
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult updateRoom(Room room){
        Integer id = room.getId();
        if(id == null){
            return AjaxResult.error("没有id");
        }
        boolean b = roomService.updateById(room);
        if(b){
            return AjaxResult.ok("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 删除一个会议室
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("id")Integer id){
        boolean flag = roomService.deleteById(id);
        if(flag){
            return AjaxResult.ok("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

}
